<?php
class Plan_Model_Plan
{
    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('非正确的table类型！');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Plan_Model_DbTable_Plan');
        }
        return $this->_dbTable;
    }

    //获取当前套餐
    public function getActivePlan($customerId)
    {
        $select = $this->getDbTable()->select()->where("customer_id = ?",$customerId)
                                               ->where("active = 1");
        return  $this->_dbTable->fetchRow($select);
    }
    
    
    //按套餐id获取套餐
    public function getPlanById($customerId)
    {
    	$select = $this->getDbTable()->select()->where("customer_id = ?",$customerId);
    	return  $this->_dbTable->fetchAll($select);
    }
    
    //按套餐id获取套餐详细
    public function getByPlanId($plan_id)
    {
    	$select = $this->getDbTable()->select()->where("plan_id = ?",$plan_id);
    	return  $this->_dbTable->fetchRow($select);
    }
	
    //获取所有当天结算的套餐
    public function getAllPlansForBillinig()
    {
    	$select = $this->getDbTable()->select()->where("billing_date = ?",date('j'))
    	                                       ->where('active = 1');
    	return  $this->_dbTable->fetchAll($select);
    }
    
    //更新套餐状态
    public function updatePlanStatus($plan_id)
    {
    	$where = $this->_dbTable->getAdapter()->quoteInto("plan_id = ?",$plan_id);
    	return $this->_dbTable->update(array("active"=>"0","expired"=>date("Y-m-d H:i:s")),$where);
    }
	

}